package net.wesc.ac.server;

import net.wesc.ac.client.MainPanel;
import net.wesc.ac.client.Service;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

import Jama.Matrix;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ServiceImpl extends RemoteServiceServlet implements
		Service {

	/**
	 * 活度系数实验数据的模型拟合函数
	 * String data: 实验数据
	 * String model：模型方程编号
	 * String type：电解质类型
	 * String temp：实验温度 
	 */
	public double[] fit(String data, String model, String type, String temp) throws IllegalArgumentException {
		double ttemp = Double.parseDouble(temp);
		LMfunc f = new LMDH(1,1,ttemp); //default 1-1 type electrolyte & DH equation
	    double[] param = null; //default inital
	    
		if(model.matches(MainPanel.DH_EQUATION)){
			if(type.matches(MainPanel.E11_TYPE)){
				f = new LMDH(1,1,ttemp);	// works
			}
			if(type.matches(MainPanel.E12_TYPE)){
				f = new LMDH(1,2,ttemp);	// works
			}
			if(type.matches(MainPanel.E13_TYPE)){
				f = new LMDH(1,3,ttemp);	// works
			}
			if(type.matches(MainPanel.E21_TYPE)){
				f = new LMDH(2,1,ttemp);	// works
			}
			if(type.matches(MainPanel.E22_TYPE)){
				f = new LMDH(2,2,ttemp);	// works
			}
			if(type.matches(MainPanel.E23_TYPE)){
				f = new LMDH(2,3,ttemp);	// works
			}
			if(type.matches(MainPanel.E31_TYPE)){
				f = new LMDH(3,1,ttemp);	// works
			}
			if(type.matches(MainPanel.E32_TYPE)){
				f = new LMDH(3,2,ttemp);	// works
			}
			if(type.matches(MainPanel.E33_TYPE)){
				f = new LMDH(3,3,ttemp);	// works
			}
		}
		
		if(model.matches(MainPanel.HE_EQUATION)){
			if(type.matches(MainPanel.E11_TYPE)){
				f = new LMHE(1,1,ttemp);
			}
			if(type.matches(MainPanel.E12_TYPE)){
				f = new LMHE(1,2,ttemp);
			}
			if(type.matches(MainPanel.E13_TYPE)){
				f = new LMHE(1,3,ttemp);
			}
			if(type.matches(MainPanel.E21_TYPE)){
				f = new LMHE(2,1,ttemp);
			}
			if(type.matches(MainPanel.E22_TYPE)){
				f = new LMHE(2,2,ttemp);
			}
			if(type.matches(MainPanel.E23_TYPE)){
				f = new LMHE(2,3,ttemp);
			}
			if(type.matches(MainPanel.E31_TYPE)){
				f = new LMHE(3,1,ttemp);
			}
			if(type.matches(MainPanel.E32_TYPE)){
				f = new LMHE(3,2,ttemp);
			}
			if(type.matches(MainPanel.E33_TYPE)){
				f = new LMHE(3,3,ttemp);
			}
		}
		
		if(model.matches(MainPanel.RS_EQUATION)){
			if(type.matches(MainPanel.E11_TYPE)){
				f = new LMRS(1,1,ttemp);
			}
			if(type.matches(MainPanel.E12_TYPE)){
				f = new LMRS(1,2,ttemp);
			}
			if(type.matches(MainPanel.E13_TYPE)){
				f = new LMRS(1,3,ttemp);
			}
			if(type.matches(MainPanel.E21_TYPE)){
				f = new LMRS(2,1,ttemp);
			}
			if(type.matches(MainPanel.E22_TYPE)){
				f = new LMRS(2,2,ttemp);
			}
			if(type.matches(MainPanel.E23_TYPE)){
				f = new LMRS(2,3,ttemp);
			}
			if(type.matches(MainPanel.E31_TYPE)){
				f = new LMRS(3,1,ttemp);
			}
			if(type.matches(MainPanel.E32_TYPE)){
				f = new LMRS(3,2,ttemp);
			}
			if(type.matches(MainPanel.E33_TYPE)){
				f = new LMRS(3,3,ttemp);
			}
		}
		
		if(model.matches(MainPanel.GS_EQUATION)){
			if(type.matches(MainPanel.E11_TYPE)){
				f = new LMGS(1,1,ttemp);
			}
			if(type.matches(MainPanel.E12_TYPE)){
				f = new LMGS(1,2,ttemp);
			}
			if(type.matches(MainPanel.E13_TYPE)){
				f = new LMGS(1,3,ttemp);
			}
			if(type.matches(MainPanel.E21_TYPE)){
				f = new LMGS(2,1,ttemp);
			}
			if(type.matches(MainPanel.E22_TYPE)){
				f = new LMGS(2,2,ttemp);
			}
			if(type.matches(MainPanel.E23_TYPE)){
				f = new LMGS(2,3,ttemp);
			}
			if(type.matches(MainPanel.E31_TYPE)){
				f = new LMGS(3,1,ttemp);
			}
			if(type.matches(MainPanel.E32_TYPE)){
				f = new LMGS(3,2,ttemp);
			}
			if(type.matches(MainPanel.E33_TYPE)){
				f = new LMGS(3,3,ttemp);
			}
		}
		
		if(model.matches(MainPanel.PP_EQUATION)){
			if(type.matches(MainPanel.E11_TYPE)){
				f = new LMPP(1,1,ttemp);
			}
			if(type.matches(MainPanel.E12_TYPE)){
				f = new LMPP(1,2,ttemp);
			}
			if(type.matches(MainPanel.E13_TYPE)){
				f = new LMPP(1,3,ttemp);
			}
			if(type.matches(MainPanel.E21_TYPE)){
				f = new LMPP(2,1,ttemp);
			}
			if(type.matches(MainPanel.E22_TYPE)){
				f = new LMPP(2,2,ttemp);
			}
			if(type.matches(MainPanel.E23_TYPE)){
				f = new LMPP(2,3,ttemp);
			}
			if(type.matches(MainPanel.E31_TYPE)){
				f = new LMPP(3,1,ttemp);
			}
			if(type.matches(MainPanel.E32_TYPE)){
				f = new LMPP(3,2,ttemp);
			}
			if(type.matches(MainPanel.E33_TYPE)){
				f = new LMPP(3,3,ttemp);
			}
		}
		
		double[][] expdata = Data.getData(data);
		int rows = (new Matrix(expdata)).getRowDimension();//获得输入数据数组的行数
		int cols = (new Matrix(expdata)).getColumnDimension();//获得输入数据数组的列数
	    double[][] x = new double[rows][cols];
	    //System.out.println(rows + "," + cols);
	    double[] y = new double[rows];
	    for(int i=0;i<rows;i++){
	    	for(int j=0;j<cols-1;j++){
	    		x[i][j] = expdata[i][j];
	    	}
    		y[i] = expdata[i][cols-1];
	    }
	    //(new Matrix(x)).print(10, 4);
	    
	    double[] aguess = f.initial();
	    
	    double[] s = new double[rows];
	    for( int i = 0; i < rows; i++ ) {
	    	  s[i] = 1.;
	    }
	    
	    boolean[] vary = new boolean[aguess.length];
	    for( int i = 0; i < aguess.length; i++ ) vary[i] = true;
	    
	    try {
	      param = LM.solve(x, aguess, y, s, vary, f, 0.001, 0.01, 100, 2);
	    }
	    catch(Exception ex) {
	      log(ex.getMessage()); 
	    }

		return param;
	}
	
	public String expPlot(String data, String model){
		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session =request.getSession();
		session.setAttribute("data", data);
		session.setAttribute("model", model);
		String id = RandomString.getString(20);
		String url = "WescAC/expplot?id=" + id;
		return url;
	}

	@Override
	public String fitPlot(String data, double[] param, String model, String type, String temp) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session =request.getSession();
		session.setAttribute("param", param);
		session.setAttribute("data", data);
		session.setAttribute("model", model);
		session.setAttribute("type", type);
		session.setAttribute("temp", temp);
		String id = RandomString.getString(20);
		String url = "WescAC/fitplot?id=" + id;
		return url;
	}

	@Override
	public String bothPlot(String data, double[] param, String model, String type, String temp)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		HttpServletRequest request = this.getThreadLocalRequest();
		HttpSession session =request.getSession();
		session.setAttribute("param", param);
		session.setAttribute("data", data);
		session.setAttribute("model", model);
		session.setAttribute("type", type);
		session.setAttribute("temp", temp);
		String id = RandomString.getString(20);
		String url = "WescAC/bothplot?id=" + id;
		return url;
	}
}
